# models/sys_user_password_history.py
from sqlalchemy import Column, String, DateTime,Boolean, Integer, ForeignKey
from sqlalchemy.orm import relationship
from src.core.ormdb import Base
from src.core import str_utils as strU


class SysUserPasswordHistory(Base):
    """最近 6 次密码历史（哈希值），用于“不能与最近 6 次相同”校验"""
    __tablename__ = "sys_user_password_history"
    __table_args__ = (
        # 每个用户最多保留 6 条
        # 数据库层可再建触发器或应用层循环删除老记录
        {'comment': '用户密码历史表，保留最近 6 次哈希'}
    )

    id = Column(String(36), primary_key=True, default=lambda: strU.generate_id())
    user_id = Column(String(36), ForeignKey("sys_user_passwords.user_id", ondelete="CASCADE"), nullable=False)
    password_hash = Column(String(255), nullable=False, comment="历史密码哈希")
    created_at = Column(DateTime, default=lambda: strU.now(), comment="该密码写入历史时间")
    create_date = Column(DateTime, default=lambda: strU.now(), comment="创建时间")
    creator_by = Column(String(255), nullable=True, comment="创建者")
    last_updated_date = Column(DateTime, nullable=True, onupdate=lambda: strU.now(), comment="最后更新时间")
    last_updated_by = Column(String(255), nullable=True, comment="最后更新者")
    # 软删除
    deletion_mark = Column(Boolean, default=False, comment="删除标志：0未删除，1已删除")
    deletion_date = Column(DateTime, nullable=True, comment="删除时间")
    deletion_by = Column(String(255), nullable=True, comment="删除者")

    # ORM 快捷导航（可选）
    # user_password = relationship("SysUserPasswords", back_populates="history_list")
